﻿using System;
using BLToolkit.Data;
using BLToolkit.DataAccess;

namespace PMIS.BLL.System
{
	/// <summary>
	/// 异常处理
	/// </summary>
	public class BL_ExceptionOperation
	{
		/// <summary>
		/// 记录异常
		/// </summary>
		/// <param name="ex">异常信息</param>
		/// <returns>系统异常ID</returns>
		public static string WriteException(Exception ex)
		{
			if (ex == null)
				return string.Empty;

			string result = string.Empty;
			DbManager dm = null;
			try
			{
				//	创建数据库操作实例
				dm = BL_DatabaseConnection.CreateConnection();

				result = dm.SetCommand("Select ID From EXCEPTIONS E Where E.MESSAGE = @MESSAGE",
					dm.Parameter("MESSAGE", ex.Message))
					.ExecuteScalar<string>();

				if (string.IsNullOrEmpty(result))
				{
					#region 新增异常

					EXCEPTIONS item = new EXCEPTIONS();
					item.ID = Guid.NewGuid().ToString("D");
					item.MESSAGE = ex.Message;
					item.STACKTRACE = ex.StackTrace;
					item.TIME = DateTime.Now;
					//	关联的记录ID
					item.RELATED_ID = null;
					//	暂不记录内部异常
					item.INNER_EXCEPTION_ID = string.Empty;
					//	异常次数
					item.COUNT = 1;

					//	异常信息记录在数据库中
					if (new SqlQuery<EXCEPTIONS>(dm).Insert(item) == 0)
						throw new Exception("新增系统异常失败!");

					result = item.ID;
					#endregion
				}
				else
				{
					#region 原有异常
					//	将异常更新为最新情况
					if (dm.SetCommand("Update EXCEPTIONS E Set E.TIME = @TM, E.COUNT = E.COUNT + 1 Where E.ID = @ID",
						dm.Parameter("TM", DateTime.Now),
						dm.Parameter("ID", result))
						.ExecuteNonQuery() == 0)
						throw new Exception("更新原有异常失败,ID:" + result);
					#endregion
				}
			}
			catch (Exception e)
			{
				BL_Logs.WriteLog(e);
				result = string.Empty;
			}
			finally
			{
				if (dm != null)
				{
					dm.Close();
					dm.Dispose();
					dm = null;
				}
			}

			return result;
		}
		/// <summary>
		/// 记录异常
		/// </summary>
		/// <param name="ex">异常信息</param>
		/// <param name="dm">数据库操作对象</param>
		/// <returns>系统异常ID</returns>
		public static string WriteException(Exception ex, DbManager dm)
		{
			if (ex == null)
				return string.Empty;

			string result = string.Empty;
			bool newCreate = false;
			try
			{
				//	创建数据库操作实例
				if (dm == null)
				{
					dm = BL_DatabaseConnection.CreateConnection();
					newCreate = true;
				}

				result = dm.SetCommand("Select ID From EXCEPTIONS E Where E.MESSAGE = @MESSAGE",
					dm.Parameter("MESSAGE", ex.Message))
					.ExecuteScalar<string>();

				if (string.IsNullOrEmpty(result))
				{
					#region 新增异常

					EXCEPTIONS item = new EXCEPTIONS();
					item.ID = Guid.NewGuid().ToString("D");
					item.MESSAGE = ex.Message;
					item.STACKTRACE = ex.StackTrace;
					item.TIME = DateTime.Now;
					//	关联的记录ID
					item.RELATED_ID = null;
					//	暂不记录内部异常
					item.INNER_EXCEPTION_ID = string.Empty;
					//	异常次数
					item.COUNT = 1;

					//	异常信息记录在数据库中
					if (new SqlQuery<EXCEPTIONS>(dm).Insert(item) == 0)
						throw new Exception("新增系统异常失败!");

					result = item.ID;
					#endregion
				}
				else
				{
					#region 原有异常
					//	将异常更新为最新情况
					if (dm.SetCommand("Update EXCEPTIONS E Set E.TIME = @TM, E.COUNT = E.COUNT + 1 Where E.ID = @ID",
						dm.Parameter("TM", DateTime.Now),
						dm.Parameter("ID", result))
						.ExecuteNonQuery() == 0)
						throw new Exception("更新原有异常失败,ID:" + result);
					#endregion
				}
			}
			catch (Exception e)
			{
				BL_Logs.WriteLog(e);
				result = string.Empty;
			}
			finally
			{
				if (dm != null && newCreate)
				{
					dm.Close();
					dm.Dispose();
					dm = null;
				}
			}

			return result;
		}
	}
}
